//
//  CDWechatLoginStep.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/20.
import Combine

struct CDWechatLoginStep: LoginStep {
    struct InputData {
        let socialCode: String
        let socialState: String
    }
    
    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.WechatLogin.Response, Error> {
        Future { promise in
            let api = AuthAPI.WechatLogin(body: .init(socialCode: input.socialCode, socialState: input.socialState, userName: nil))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                    case .success(let response):
                    if let response = response.data
                    {
                        Logger.log(.info, message: "微信登陆成功: \(response)",tag: .login)
                        promise(.success(response))
                    }
                    else{
                        promise(.failure(NSError(domain: "微信登录", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                    }
                    case .failure(let error):
                        Logger.log(.error, message: "微信登陆失败: \(error)", tag: .login)
                        promise(.failure(error))
                    }
            })
        }
        .eraseToAnyPublisher()
    }
}
